package com.wdc.application;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

public class AQSDemo {

    private final   Semaphore semaphore = new Semaphore(5);

    public void processPayment(String orderId) {

        try {
            semaphore.acquire();
            System.out.println("processing payment for order " + orderId);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            semaphore.release(); //释放信号量
        }
    }


    public static void main(String[] args) {
        AQSDemo aqsDemo = new AQSDemo();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 1; i <= 10; i++) {
             String orderId = "Order -" + i;
            executorService.execute(() -> aqsDemo.processPayment(orderId));

        }


    }
}
